package com.example.spsaveqqty.attraction;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.spsaveqqty.R;

import java.util.List;

public class RankActivity extends AppCompatActivity {

    private LinearLayout ranklist;
    private LinearLayout oneAttrac;
    private LinearLayout describe;
    private ImageView imageView;
    private TextView attracName;
    private TextView tip;
    private TextView address;
    private TextView score;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank);
        listrank();
    }


    public void listrank(){

        ranklist = findViewById(R.id.ranklist);


        Intent i = getIntent();
        List<Attraction> rankList = (List<Attraction>) i.getSerializableExtra("rankList");

        for(final Attraction rank:rankList) {
            final String name = rank.getAttract_name();
            oneAttrac = new LinearLayout(this);
            describe = new LinearLayout(this);
            imageView = new ImageView(this);
            tip = new TextView(this);
            attracName = new TextView(this);
            address = new TextView(this);
            score = new TextView(this);


            //初始化样式
            initView(oneAttrac, describe, imageView);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0,30,0,0);
            score.setLayoutParams(params);
            score.setTextColor(Color.parseColor("#e7933a"));
            tip.setLayoutParams(params);
            address.setLayoutParams(params);

            // 使用代码设置drawableleft
            Drawable drawable = getResources().getDrawable(
                    R.drawable.icon_address);
            // / 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getIntrinsicHeight(),
                    drawable.getIntrinsicWidth());
            address.setCompoundDrawables(drawable, null, null, null);
//            ratingBar.setNumStars(3);

            attracName.setTextSize(20);
            TextPaint tp = attracName.getPaint();
            tp.setFakeBoldText(true);


            Glide.with(this).load(rank.getPic()).into(imageView);

            attracName.setText(rank.getAttract_name());
            tip.setText(rank.getTip());
            address.setText(rank.getAddress());
            score.setText(rank.getScore().toString()+"分");


            describe.addView(attracName);
            describe.addView(score);
            describe.addView(tip);
            describe.addView(address);
            oneAttrac.addView(imageView);
            oneAttrac.addView(describe);
            ranklist.addView(oneAttrac);

            //点击进入详情
            oneAttrac.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    fingByName(name);
                }
            });

        }
    }

    //访问数据库
    public void fingByName(String name){
        AttractionDao dao = new AttractionDao(RankActivity.this);
        List<Attraction> attracList = null;
        attracList = dao.select(name);

        Intent intent = new Intent(RankActivity.this, InformationActivity.class);
        intent.putExtra("attraction", attracList.get(0));
        startActivity(intent);
    }


    public void initView(LinearLayout oneAttrac, LinearLayout describe, ImageView imageView){

        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,450);
        param.setMargins(0,45,0,0);
        oneAttrac.setLayoutParams(param);
        oneAttrac.setOrientation(LinearLayout.HORIZONTAL);


        LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                     LinearLayout.LayoutParams.MATCH_PARENT);
        param4.setMargins(20,0,0,0);
        describe.setLayoutParams(param4);
        describe.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(400,
                400);//两个400分别为添加图片的大小
//        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);


    }
}
